package juego {
	import auxiliares.Coordenada;
	import auxiliares.Grafico;
	import auxiliares.SemiCircle;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.core.UIComponent;
	import mx.effects.Move;
	import mx.events.EffectEvent;
	import mx.managers.CursorManager;

	/**
	 * 
	 * @author mcustiel
	 * 
	 */
	public class Barco extends Grafico {
		private var idBarco: String;
		private var idBando: String;
		private var idTipoBarco: String;
		private var blindaje: Number;
		private var vida: Number;
		private var enDoceMillas: String;
		private var enPuerto: Number;
		private var puerto: String;
		private var areaVisible: Number;
		private var jump: Boolean;
		private var armas: Array;

		/**
		 * 
		 * @param _idBarco
		 * @param _idBando
		 * @param _contenedor
		 * @param _imagen
		 * @param _callback
		 * 
		 */
		public function Barco(_idBarco: String, _idTipoBarco: String, _idBando: String, _contenedor:Canvas, _imagen: Object, _xPos:Number = 0, _yPos:Number = 0, _ancho:Number = 0, _alto:Number = 0, _callback: Function = null) {
			super(_contenedor, _imagen, _xPos, _yPos, _ancho, _alto);
			
			armas = new Array();
			idBando = _idBando;
			idBarco = _idBarco;
			idTipoBarco = _idTipoBarco;
			contenedorImagen.id = idBarco;
			contenedorImagen.name = idBando;
			imagen.id = "img_" +  idBarco;
			imagen.name = "img_" +  idBando;
			if (_callback != null) {
				imagen.addEventListener(MouseEvent.MOUSE_UP, _callback);
			}
			setTimePixel(300);
			setTimePixelRotate(200);
			enPuerto = 0;
			puerto = "";
			jump = false;
			enDoceMillas = new String();
			blindaje = new Number();
			vida = new Number();
			areaVisible = 350;
		}

		public function setArma(_id: Number, _cantidad: Number): void {
			armas[_id] = _cantidad;
		}
		
		public function getArma(_id: Number): Number {
			return armas[_id];
		}
		
		public function getAreaVisible(): Number {
			return areaVisible;
		}
		
		public function setAreaVisible(_value: Number): void {
			areaVisible = _value;
		}
		
		public function getBlindaje():Number {
			return blindaje;
		}
		
		public function setBlindaje(value:Number):void {
			blindaje = value;
		}
		
		public function getVida():Number {
			return vida;
		}
		
		public function setVida(value:Number):void {
			vida = value;
		}

		public function getNombreBando(): String {
			return idBando;
		}
		
		public function setNombreBando(_bando: String ): void {
			idBando = _bando;
		}
		
		public function getNombreBarco(): String {
			return idBarco;
		}
		
		public function setNombreBarco(_nombre: String): void {
			idBarco = _nombre;
		}
		
		public function getTipoBarco(): String{
			return idTipoBarco;
		}
		
		public function setEnDoceMillas(_en: String): void {
			enDoceMillas = _en;
		}
		
		public function getEnDoceMillas(): String {
			return enDoceMillas;
		}
		
		public function setEnPuerto(_en: Number, _puerto:String = null): void {
			enPuerto = _en;
			if ( _puerto ) puerto = _puerto;
		}
		
		public function getEnPuerto(): Number {
			return enPuerto;
		}
		
		public function getPuerto(): String {
			return puerto;
		}
		
		public function setJump(_flag: Boolean): void {
			jump = _flag;
		}
		
		public function getJump(): Boolean {
			return jump;
		}
		
		public function detener(): void {
			super.stopRotate();
			super.stopMove();
		}

/*		override public function mover(_newX:Number, _newY:Number): void {
			super.mover(_newX, _newY);			
		}
	*/	
		public function calcularPosBarco( _tipo:Number): Coordenada {
			var grad:Number = getAngulo();
			var ancho:Number = getAncho();
			var gradTan:Number;
			var x:Number = getXPos();
			var y:Number = getYPos();
			
			if ( grad >= 270 ) {
				gradTan = 360 - grad;
			} else if ( grad >= 180 ) {
				gradTan = grad - 180;
			} else if ( grad >= 90 ) {
				gradTan = 180 - grad;
			} else {
				gradTan = grad;
			}
			//trace("pos arma gT: "+gradTan+" g: "+grad+" tipo: "+_tipo+" ancho: "+ancho);
			var radianes:Number = gradTan * (Math.PI / 180);
			var newX:Number;
			var newY:Number;
			var h:Number;
			switch (_tipo) {
				case 3:
					//la punta
					h = ancho/2;
					newX = Math.abs(Math.cos(radianes) * h);
					newY = Math.abs(Math.sin(radianes) * h);
				break;
				case 2:
					//primera artilleria
					h = ancho/4;
					newX = Math.abs(Math.cos(radianes) * h);
					newY = Math.abs(Math.sin(radianes) * h);
				break;
				case 1:
					//segunda artilleria
					h = ancho/2;
					newX = Math.abs(Math.cos(radianes) * h) * -1;
					newY = Math.abs(Math.sin(radianes) * h) * -1;
				break;
				case 4:
					// punto frontal
					h = ancho;
					newX = Math.abs(Math.cos(radianes) * h);
					newY = Math.abs(Math.sin(radianes) * h);
				break;
			}
			
			if (grad <= 90) {
				x += newX;
				y += newY;
			} else if (grad <= 180) {
				x -= newX;
				y += newY;
			} else if (grad <= 270) {
				x -= newX;
				y -= newY;
			} else {
				x += newX;
				y -= newY;
			}
			//trace("pos arm x: "+x+" y: "+y);
			return new Coordenada(x, y);
		}
		
		public function calcularDestino(_newX:Number, _newY:Number): Coordenada {
			var grados:Number = calcularAngulo(_newX, _newY);
			var gradTan:Number;
			if ( grados >= 270 ) {
				gradTan = 360 - grados;
			} else if ( grados >= 180 ) {
				gradTan = grados - 180;
			} else if ( grados >= 90 ) {
				gradTan = 180 - grados;
			} else {
				gradTan = grados;
			}
			var radianes:Number = gradTan * (Math.PI / 180);
			//trace ("xTo: "+_newX+" yTo: "+_newY+" anguloTan: "+gradTan);
			
			var oldX:Number = getXPos();
			var oldY:Number = getYPos();
			var h:Number = Math.sqrt(Math.pow((_newX - oldX), 2) + Math.pow((_newY - oldY), 2));
			var newX:Number;
			var newY:Number;
			/*if ( h > velocidad ) {
				newX = Math.abs(Math.cos(radianes) * velocidad);
				newY = Math.abs(Math.sin(radianes) * velocidad);
			} else {*/
				newX = Math.abs(_newX - oldX);
				newY = Math.abs(_newY - oldY);
			//}
			
			newX = Math.round(newX);
			newY = Math.round(newY);
			//trace ( "newX: "+newX+" newY: "+newY);
			if (grados <= 90) {
				oldX += newX;
				oldY += newY;
			} else if (grados <= 180) {
				oldX -= newX;
				oldY += newY;
			} else if (grados <= 270) {
				oldX -= newX;
				oldY -= newY;
			} else {
				oldX += newX;
				oldY -= newY;
			}
			return new Coordenada(oldX, oldY);
		}
		
		public function calcularRetroceso(_newX:Number, _newY:Number): Coordenada {
			var oldX:Number = getXPos();
			var oldY:Number = getYPos();
			var ang:Number = getAngulo();
			if (ang <= 90) {
				oldX -= _newX;
				oldY -= _newY;
			} else if (ang <= 180) {
				oldX += _newX;
				oldY -= _newY;
			} else if (ang <= 270) {
				oldX += _newX;
				oldY += _newY;
			} else {
				oldX -= _newX;
				oldY += _newY;
			}
			return new Coordenada(oldX, oldY);
		}
		
		public function agregarAreaVisibilidad(_call:Function):void {
			
			var name:String = idBarco + "_visibilidad";
			eliminarAreaVisibilidad();	
			var wrapper : UIComponent = new UIComponent();
			wrapper.id = idBarco + "_visibilidad";
			wrapper.name = idBarco + "_visibilidad";
			var circle:Sprite = new Sprite();
			circle.name = "AreaVisiblidad_" + idBarco ;
			circle.graphics.beginFill(0x99CCFF, 0.15);
			circle.graphics.drawCircle(0, 0, areaVisible);
			circle.graphics.endFill();
			circle.addEventListener(MouseEvent.MOUSE_OVER, function(event:MouseEvent):void {
				_call("mover");
			});
			circle.addEventListener(MouseEvent.MOUSE_OUT, function(event:MouseEvent):void {
				_call("normal");
			});
			wrapper.addChild(circle);
			contenedorImagen.addChildAt(wrapper,0);
		}
		
		public function tieneAreaVisibilidad(): Boolean {
			return getAreaVisibilidad()!=null;
		}
		
		public function getAreaVisibilidad(): DisplayObject {
			var name:String = idBarco + "_visibilidad";
			var circle:DisplayObject = contenedorImagen.getChildByName(name);
			return circle;
		}
		
		public function eliminarAreaVisibilidad():void {
			var name:String = idBarco + "_visibilidad";
			var circle:DisplayObject = contenedorImagen.getChildByName(name);
			if ( circle ) {
				contenedorImagen.removeChild(circle);
			}
		}
		
		public function agregarAreaDisparo(_type:String):void {
			var name:String = idBarco + "_disparo";
			eliminarAreaDisparo();	
			var semi:Object = new SemiCircle();
			var wrapper : UIComponent = new UIComponent();
			wrapper.id = idBarco + "_disparo";
			wrapper.name = idBarco + "_disparo";
			var circle:Sprite = new Sprite();
			circle.name = "AreaDisparo_" + idBarco;
			if( _type != "1") {
				semi.drawSemiCircle(circle.graphics, 0, 0, 270, 90, 100, 1);
			} else {
				semi.drawSemiCircle(circle.graphics, 0, 0, 90, 270, 100, 1);
			}
			wrapper.addChild(circle);
			contenedorImagen.addChildAt(wrapper, 1);
		}
		
		public function tieneAreaDisparo(): Boolean {
			var name:String = idBarco + "_disparo";
			var circle:DisplayObject = contenedorImagen.getChildByName(name);
			return circle!=null;
		}
		
		public function eliminarAreaDisparo():void {
			var name:String = idBarco + "_disparo";
			var circle:DisplayObject = contenedorImagen.getChildByName(name);
			if ( circle ) {
				contenedorImagen.removeChild(circle);
			}
		}
		
		override public function eliminar(_type:String = null):void {
			eliminarFunction(idBarco, _type);
			if ( _type == null ) super.eliminar();
		}
	}
}